﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class LapPhieuBanHang : Form
    {
        double sum = 0;
        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        NhanVien nv = new NhanVien();
        public LapPhieuBanHang()
        {
            InitializeComponent();
        }

        public void Set(NhanVien n)
        {
            nv = n;
            LoaiNhanVien lnv = ws.NhanVien_LayLoaiNV(nv);
            label4.Text = lnv.LoaiNhanVien_Ten + ":";
            label5.Text = nv.NhanVien_TenDayDu;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                textBox3.Enabled = true;
            else
                textBox3.Enabled = false;
            TinhTongTien();
            if (textBox3.Text == "" || textBox2.Text == "")
                return;
            double temp;
            if (ws.KiemTraDouble(textBox3.Text))
                temp = double.Parse(textBox3.Text);
            else
                return;
            //double temp2 = double.Parse(textBox2.Text);
            if (temp > 100 && radioButton1.Checked)
            {
                MessageBox.Show("Phần trăm không thể lớn hơn 100");
                textBox3.Focus();
                return;
            }
            else if (temp > sum + temp)
            {
                MessageBox.Show("Khuyến mãi không thể lớn hơn giá bán");
                textBox3.Focus();
                return;
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "")
            {
                MessageBox.Show("Nhập hoặc quét mã hàng.");
                return;
            }
            SanPham temp = new SanPham();
            temp.SanPham_MaHang = textBox1.Text;

            SanPham sp = ws.SanPham_LaySP(temp);
            if(sp == null)
            {
                MessageBox.Show("Sản phẩm không tồn tại");
                return;
            }
            if(sp.SanPham_SoLuong == 0)
            {
                MessageBox.Show("Sản phẩm này đã hết hàng.");
                return;
            }

            int r = CheckDatagrid(sp);
            if (r >= 0)
            {
                int i = (int)dataGridView1.Rows[r].Cells[9].Value;
                if(i >= sp.SanPham_SoLuong)
                {
                    MessageBox.Show("Số lượng sản phẩm này đã bằng lượng trong kho.");
                    return;
                }
                LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                dataGridView1.Rows[r].SetValues(r + 1, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, i + 1);
            }
            else
            {
                LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, 1);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = sp;
            }
            TinhTongTien();
        }

        int CheckDatagrid(SanPham sp)
        {
            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                SanPham temp = (SanPham)row.Tag;
                if (temp.MaSanPham == sp.MaSanPham)
                    return row.Index;
            }
            return -1;
        }

        private void TinhTongTien()
        {
            sum = 0;
            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                SanPham sp = (SanPham)row.Tag;
                if (sp.SanPham_GiaUuDai != null)
                {
                    if (sp.SanPham_GiaUuDai > 0)
                        sum += (double)sp.SanPham_GiaUuDai * (int)row.Cells[9].Value;
                }
                else
                    sum += (double)sp.SanPham_GiaKhiBan * (int)row.Cells[9].Value;
            }
            textBox2.Text = sum.ToString();
            if (checkBox1.Checked)
            {
                if (textBox3.Text != "" && textBox2.Text != "")
                {
                    double temp;
                    if(ws.KiemTraDouble(textBox3.Text))
                        temp = double.Parse(textBox3.Text);
                    else
                    {
                        temp = 0;
                    }

                    double temp2 = double.Parse(textBox2.Text);
                    if (radioButton1.Checked)
                    {
                        temp2 -= temp2 * temp / 100;
                        sum = temp2;
                        textBox2.Text = sum.ToString();
                    }
                    else
                    {
                        temp2 -= temp;
                        if (temp2 > 0)
                        {
                            sum = temp2;
                            textBox2.Text = sum.ToString();
                        }
                    }
                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
                e.SuppressKeyPress = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                if (cell == null)
                    continue;
                int iIndex = cell.RowIndex;
                if(0 <= iIndex && iIndex < dataGridView1.Rows.Count)
                    if (dataGridView1.Rows[iIndex] != null)
                        dataGridView1.Rows.RemoveAt(iIndex);
            }
            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                row.SetValues(row.Index + 1);
            }
            TinhTongTien();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            /*if (textBox3.Text == "" || textBox2.Text == "")
                return;
            double temp = double.Parse(textBox3.Text);
            double temp2 = double.Parse(textBox2.Text);
            if(radioButton1.Checked)
            {
                temp2 -= temp2 * temp / 100;
                sum = temp2;
                textBox2.Text = sum.ToString();
            }
            else
            {
                temp2 -= temp;
                if(temp2 > 0)
                {
                    sum = temp2;
                    textBox2.Text = sum.ToString();
                }
            }*/
            TinhTongTien();
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            if(textBox3.Text == "" || textBox2.Text == "")
                return;
            double temp;
            if (ws.KiemTraDouble(textBox3.Text))
                temp = double.Parse(textBox3.Text);
            else
            {
                MessageBox.Show("Khuyến mãi không hợp lệ");
                textBox3.Focus();
                return;
            }
            //double temp2 = double.Parse(textBox2.Text);
            if(temp > 100 && radioButton1.Checked)
            {
                MessageBox.Show("Phần trăm không thể lớn hơn 100");
                textBox3.Focus();
                return;
            }
            else if(temp > sum + temp)
            {
                MessageBox.Show("Khuyến mãi không thể lớn hơn giá bán");
                textBox3.Focus();
                return;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (textBox3.Text == "" || textBox2.Text == "")
                return;
            double temp;
            if (ws.KiemTraDouble(textBox3.Text))
                temp = double.Parse(textBox3.Text);
            else
            {
                MessageBox.Show("Khuyến mãi không hợp lệ");
                textBox3.Focus();
                return;
            }
            //double temp2 = double.Parse(textBox2.Text);
            if (temp > 100 && radioButton1.Checked)
            {
                MessageBox.Show("Phần trăm không thể lớn hơn 100");
                textBox3.Focus();
                return;
            }
            else if (temp > sum + temp)
            {
                MessageBox.Show("Khuyến mãi không thể lớn hơn giá bán");
                textBox3.Focus();
                return;
            }
            TinhTongTien();
        }

        private void LapPhieuBanHang_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("Chưa có sản phẩm nào.");
                return;
            }
            TinhTongTien();
            /*
            HoaDonBanHang hdbh = new HoaDonBanHang();
            hdbh.HDBH_NgayLap = dateTimePicker1.Value;
            hdbh.MaNhanVien = nv.MaNhanVien;
            hdbh.HDBH_ThanhTien = sum;
            hdbh.HDBH_TrangThai = "Su dung";

            if(!ws.HoaDonBanHang_Them(hdbh))
            {
                MessageBox.Show("Thất bại");
                return;
            }

            hdbh = new HoaDonBanHang();
            hdbh = ws.HoaDonBanHang_LayHDGanNhat();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                SanPham sp = (SanPham)row.Tag;
                ChiTietHoaDonBanHang cthdbh = new ChiTietHoaDonBanHang();
                cthdbh.MaHoaDon = hdbh.MaHoaDon;
                cthdbh.MaSanPham = sp.MaSanPham;
                if(sp.SanPham_GiaUuDai == null)
                    cthdbh.CTHDBH_DonGia = sp.SanPham_GiaKhiBan;
                else
                    cthdbh.CTHDBH_DonGia = sp.SanPham_GiaUuDai;
                cthdbh.CTHDBH_SoLuong = (int)row.Cells[9].Value;
                cthdbh.CTHDBH_TrangThai = "Su dung";

                ws.ChiTietHoaDonBanHang_Them(cthdbh);
                sp.SanPham_SoLuong -= (int)row.Cells[9].Value;
                ws.SanPham_CapNhat(sp);
            }
            MessageBox.Show("Thành công.");*/
            InHoaDonBanHang ihdbh = new InHoaDonBanHang();
            string km = "";
            if (checkBox1.Checked)
            {
                km = "Khuyến mãi " + textBox3.Text.ToString();
                if (radioButton1.Checked)
                    km += " %";
                else
                    km += " USD";
            }
            ihdbh.Set(nv, dataGridView1, dateTimePicker1.Value, sum, km);
            ihdbh.ShowDialog();
            button4.PerformClick();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            //textBox3.Clear();
            //checkBox1.Checked = false;

            dataGridView1.Rows.Clear();
            textBox1.Focus();
        }
    }
}
